#include "stdafx.h"
#include "CRGBStream.h"


NAMESPACE_BEGIN

#define DEFAULT_WIDTH 16
#define DEFAULT_HEIGHT 16
#define DEFALUT_BYTE_VALUE 0x88



_CRGBStreamW::_CRGBStreamW( _PicType ptType/* = PT_UNKNOW*/)
{
	m_ptType = ptType;
	m_pPointer = NULL;
}

_CRGBStreamW::~_CRGBStreamW()
{
	if(m_pPointer)
		delete []m_pPointer;
}

VOID _CRGBStreamW::LoadDefault()
{
	m_pPointer = new BYTE[DEFAULT_WIDTH * DEFAULT_HEIGHT]; // 16 * 16
	memset(m_pPointer, DEFALUT_BYTE_VALUE, DEFAULT_WIDTH * DEFAULT_HEIGHT);
	m_dwByteNum = DEFAULT_WIDTH * DEFAULT_HEIGHT * 4;
	m_dwWidth = DEFAULT_WIDTH;
	m_dwHeight = DEFAULT_HEIGHT;
}
VOID _CRGBStreamW::LoadJPEG(BYTE * pBuffer, DWORD dwBufferSize)
{
	jpeg_image_info info;
	::jpeg_create(pBuffer, dwBufferSize, &m_pPointer, &info);
	m_dwWidth = info.dwWidth;
	m_dwHeight = info.dwHeight;
	m_dwByteNum = info.dwByteNum;
}

VOID _CRGBStreamW::LoadDDS(BYTE * pBuffer, DWORD dwBufferSize)
{
	dds_image_info info;
	::dds_create(pBuffer, dwBufferSize, &m_pPointer, &info);
	m_dwWidth = info.dwWidth;
	m_dwHeight = info.dwHeight;
	m_dwByteNum = info.dwByteNum;
}

VOID _CRGBStreamW::LoadPNG(BYTE * pBuffer, DWORD dwBufferSize)
{
	png_image_info info;
	::png_create(pBuffer, dwBufferSize, &m_pPointer, &info);
	m_dwWidth = info.dwWidth;
	m_dwHeight = info.dwHeight;
	m_dwByteNum = info.dwByteNum;
}


VOID _CRGBStreamW::LoadBMP(BYTE * pBuffer, DWORD dwBufferSize)
{
	bmp_image_info info;
	::bmp_create(pBuffer, dwBufferSize, &m_pPointer, &info);
	m_dwWidth = info.dwWidth;
	m_dwHeight = info.dwHeight;
	m_dwByteNum = info.dwByteNum;
}
VOID _CRGBStreamW::LoadTGA(BYTE * pBuffer, DWORD dwBufferSize)
{
	tga_image_info info;
	::tga_create(pBuffer, dwBufferSize, &m_pPointer, &info);
	m_dwWidth = info.dwWidth;
	m_dwHeight = info.dwHeight;
	m_dwByteNum = info.dwByteNum;
}
VOID _CRGBStreamW::Load(BYTE * pBuffer, DWORD dwBufferSize)
{
	if(!pBuffer)
	{
		return;
	}
	if(m_ptType == PicT_UNKNOW)
	{
		if(is_jpeg_data(pBuffer, dwBufferSize))
			m_ptType = PT_JPEG;
		else if(is_png_data(pBuffer, dwBufferSize))
			m_ptType = PT_PNG;
		else if(is_dds_data(pBuffer, dwBufferSize))
			m_ptType = PT_DDS;
		else if(is_bmp_data(pBuffer, dwBufferSize))
			m_ptType = PT_BMP;
		else if(is_tga_data(pBuffer, dwBufferSize))
			m_ptType = PT_TGA;
		else
			m_ptType = PicT_UNKNOW;
	}

	switch(m_ptType)
	{
	case PT_JPEG:
		this->LoadJPEG(pBuffer, dwBufferSize);
		break;
	case PT_PNG:
		this->LoadPNG(pBuffer, dwBufferSize);
		break;
	case PT_DDS:
		this->LoadDDS(pBuffer, dwBufferSize);
		break;
	case PT_BMP:
		this->LoadBMP(pBuffer, dwBufferSize);
		break;
	case PT_TGA:
		this->LoadTGA(pBuffer, dwBufferSize);
		break;
	case PicT_UNKNOW:
		this->LoadDefault();
		break;
	}
}
VOID _CRGBStreamW::Load(_CFileStreamW * pFile)
{
	if(!pFile || pFile->GetState() != FS_OK)
	{
		return;
	}
	BYTE * pBuffer = new BYTE[(UINT_X)pFile->Size()];
	pFile->ReadFull(pBuffer);
	this->Load(pBuffer, (UINT_X)pFile->Size());
	delete [] pBuffer;
}
VOID _CRGBStreamW::Load(const WCHAR * szFileName)
{
	_CFileStreamW * pFile = _CFile::Open(szFileName, FOM_READ, FSM_READ, FM_OPEN, 0);
	if(!pFile || pFile->GetState() != FS_OK)
	{
		return;
	}

	this->Load(pFile);

	pFile->Release();
}


NAMESPACE_END